// Per Vertex ADS Lighting model (Gouraud shading) - based on: 
//    RealTime Rendering class notes (Celes, W. 2014) 
//    Mathematics for 3D Game Programming and CG, 3th (Lengyel)
//    OpenGL 4.0 Shading Cookbook (Wolff)
//    OpenGL Super Bible 6th (Sellers, Wright, Haemel)

#version 440

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;


uniform mat4 modelView;
uniform mat3 normalMatrix;
uniform mat4 projection;
uniform vec4 lsPos;   //light source position in eye coord
uniform vec4 objColor;

uniform vec4 ambientColor = vec4 (0);
uniform vec4 diffuseColor = vec4 (1.0);
uniform vec4 specularColor = vec4 (1.0);
uniform float shiness = 50;

out VS_OUT{
   vec4 vC;
} vsOut;

void main ()
{

   vec3 vN = normalize (normalMatrix*normal); // N - normal vector
   vec4 vPosition4 = (modelView)*vec4 (position, 1.0);
   vec3 vV = normalize (-vPosition4.xyz); // V - viewer vector
   vec3 vL = normalize (lsPos.xyz - vPosition4.xyz); // L - light vector


   float diff = max (0.0, dot (vL, vN));
   vsOut.vC = (diff*diffuseColor + ambientColor)*objColor;

   vec3 vReflection = normalize (reflect (-vL, vN)); // Direct reflection vector: R = 2(N.L)N - L 
   float spec = max (0.0, dot (vV, vReflection));

   if (diff > 0) {
      float fspec = pow (spec, shiness);
      vsOut.vC += vec4 (fspec*vec3 (specularColor), 1.0);
   }   

   gl_Position = projection*modelView*vec4 (position, 1.0);
}